import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:thingskit/Pages/DeviceSetting/deviceSettingPage.dart';
import 'package:thingskit/components/aRowBottons.dart';
import 'package:thingskit/components/selectBox.dart';
import 'package:thingskit/components/singleRowCard.dart';
import 'package:thingskit/utils/const.dart';

import '../../components/functionButton.dart';
import '../../components/lightContent.dart';
import '../../utils/ToastController.dart';
import '../../utils/UsbController.dart';

class DeviceGroupPage extends StatefulWidget {
  @override
  _DeviceGroupPageState createState() => _DeviceGroupPageState();
}

class _DeviceGroupPageState extends State<DeviceGroupPage> {
  final UsbController usbController = Get.find();
  bool _isOnlySearchNewLights = false;
  String? _selectedAddress;
  String _selectedGroup = '请选择分组';
  String _group = '组别';
  String _selectedFloor = '楼层';
  double progress = 0;
  List<String> level = ['低', '中', '高'];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(title: Text('设备分组')),
      body: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Obx(
              () => ARowBottons(
                title1: usbController.isDebugging.value ? '关闭串口调试' : '开启串口调试',
                color1:
                    usbController.isDebugging.value ? Colors.red : Colors.green,
                onPressed1: usbController.openSerialPortDebug,
                title2: '上下翻转',
                color2: Colors.purple,
                onPressed2: usbController.turnOver,
              ),
            ),
            Row(
              children: [
                Expanded(
                  flex: 3,
                  child: Row(
                    children: [
                      Expanded(
                        child: Row(
                          children: [
                            Text('分组', style: TextStyle(fontSize: 16)),
                            SizedBox(width: 6),
                            SelectBox(
                              initialValue: _selectedGroup,
                              options: groupOptions,
                              label: '请选择分组',
                              onChanged: (value) {
                                setState(() {
                                  _selectedGroup = value;
                                });
                              },
                            ),
                          ],
                        ),
                      ),
                      Row(
                        children: [
                          Switch(
                            materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                            value: _isOnlySearchNewLights,
                            onChanged: (value) {
                              setState(() {
                                _isOnlySearchNewLights = value;
                              });
                            },
                            activeColor: Colors.blue,
                            thumbColor: WidgetStateProperty.all(Colors.white),
                            activeTrackColor: Colors.blue,
                            inactiveTrackColor: Colors.grey,
                          ),
                          Text('只搜新灯', style: TextStyle(fontSize: 12)),
                        ],
                      )
                    ],
                  ),
                ),
                Expanded(
                  flex: 1,
                  child: FunctionButton(
                    onPressed: () {
                      _isOnlySearchNewLights
                          ? usbController.searchNewLight()
                          : usbController.searchLight();
                    },
                    title: '搜灯',
                    color: Colors.blue,
                  ),
                ),
              ],
            ),
            Expanded(
              child: Obx(
                () => ListView.builder(
                  itemCount: usbController.Lights.length,
                  itemBuilder: (context, index) {
                    final light = usbController.Lights[index];
                    return SingleRowCard(
                      content: LightContent(index: index),
                      isChecked: _selectedAddress == light['addr'],
                      onCheckChange: (newValue) {
                        setState(() {
                          _selectedAddress =
                              newValue == true ? light['addr'] : null;
                        });
                      },
                      onClick: () {
                        usbController.lightInfo(light['addr']);
                      },
                    );
                  },
                ),
              ),
            ),
            Expanded(
              child: Column(
                children: [
                  Center(
                    child: Obx(
                      () => Text(
                        '共${usbController.Lights.length}个灯',
                        style: TextStyle(fontSize: 18),
                      ),
                    ),
                  ),
                  ARowBottons(
                    title1: '闪一闪',
                    color1: Colors.green,
                    onPressed1: () => usbController.flash(_selectedAddress),
                    title2: '停止闪',
                    color2: Colors.red,
                    onPressed2: () => usbController.stopFlash(_selectedAddress),
                  ),
                  SizedBox(height: 4),
                  Column(
                    mainAxisSize: MainAxisSize.min,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        '发射功率：${level[progress.toInt()]}',
                        style: TextStyle(fontSize: 16),
                      ),
                      Slider(
                        min: 0,
                        max: 2.9,
                        value: progress,
                        activeColor: Colors.blue,
                        onChanged: (value) {
                          setState(() {
                            progress = value;
                          });
                        },
                      ),
                    ],
                  ),
                  Row(
                    children: [
                      Expanded(
                        child: Row(
                          children: [
                            Text('组号', style: TextStyle(fontSize: 16)),
                            SizedBox(width: 8),
                            SelectBox(
                              initialValue: _group,
                              options: groupOptions,
                              label: '组别',
                              onChanged: (value) {
                                setState(() {
                                  _group = value;
                                });
                              },
                            ),
                            SizedBox(width: 8),
                            SelectBox(
                              initialValue: _selectedFloor,
                              options: floorOptions,
                              label: '楼层',
                              onChanged: (value) {
                                setState(() {
                                  _selectedFloor = value;
                                });
                              },
                            ),
                          ],
                        ),
                      ),
                      SizedBox(width: 10),
                      Expanded(
                        child: FunctionButton(
                          onPressed: () => usbController.sendGroup(_group),
                          title: '下发',
                          color: Colors.blue,
                        ),
                      ),
                    ],
                  ),
                  SizedBox(height: 4),
                  ARowBottons(
                    title1: '选中',
                    color1: Colors.green,
                    onPressed1:
                        () => usbController.inviteGroup(_selectedAddress),
                    title2: '取消选中',
                    color2: Colors.orange,
                    onPressed2:
                        () => usbController.unInviteGroup(_selectedAddress),
                  ),
                  SizedBox(height: 4),
                  ARowBottons(
                    title1: '设备配置',
                    color1: Colors.lightBlueAccent,
                    onPressed1: () => Get.to(() => DeviceSettingPage()),
                    title2: '全部取消',
                    color2: Colors.red,
                    onPressed2: usbController.cancelAllGroupInvite,
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
